// JavaScript Document

/*
* album slider
* Jianyu
* Copyright@2010-2011
*/

(function($){
	$.fn.albumslider = function(options) {
		var images = [];
		var imgs = [];
		var nav_items_cont = "";
		var nav_items_count = 0;
		var nav_items_cc = 0;
		var nav_items_nc = 0;
		var nav_item_width = 125;
		var nav_item_height = 100;
		
		var nav_loop = 0;
		
		//
		function init(){
			nav_items_count = $('#albumslider .albumslider-nav-items li').length;
			if(nav_loop==1){
				nav_items_cont = $('#albumslider .albumslider-nav-items ul').html();
				$('#albumslider .albumslider-nav-items ul').html(nav_items_cont+""+nav_items_cont);
				$('#albumslider .albumslider-nav-items ul').css("width",nav_items_count*2*nav_item_width);
			} else {
				$('#albumslider .albumslider-nav-items ul').css("width",nav_items_count*nav_item_width);
			}
			//
			$('#albumslider .albumslider-nav-left').hover(
									function(){$(this).addClass("albumslider-nav-left-over");},
									function(){$(this).removeClass("albumslider-nav-left-over");});
			$('#albumslider .albumslider-nav-right').hover(
									function(){$(this).addClass("albumslider-nav-right-over");},
									function(){$(this).removeClass("albumslider-nav-right-over");});
			//
			$('#albumslider .albumslider-image').click(function(){
						showNextImage();
					});
			$('#albumslider .albumslider-nav-left').click(function(){
						showPrevImage();
					});
			$('#albumslider .albumslider-nav-right').click(function(){
						showNextImage();
					});
			$('#albumslider .albumslider-nav-items li').each(function(i){
					$(this).hover(
							function(){$(this).addClass("albumslider-nav-items-over");},
							function(){$(this).removeClass("albumslider-nav-items-over");});
					$(this).click(function(){
							nav_items_nc = $('img',this).attr("eid")-1;
							moveNavImg();
							showActiveImage();
									   });
							});
			
		}
		//
		function showPrevImage(){
			nav_items_nc = nav_items_nc - 1;
			if( nav_items_nc < 0 ){
				nav_items_nc = 0;
			}
			moveNavImg();
			showActiveImage();
		}
		function showNextImage(){
			nav_items_nc = nav_items_nc + 1;
			if( nav_loop == 1 ) {
				if( nav_items_nc >= nav_items_count ){
					nav_items_nc = 0;
					$('#albumslider .albumslider-nav-items').scrollLeft(0);
				}
			} else {
				if( nav_items_nc >= nav_items_count ){
					nav_items_nc = nav_items_count - 1 ;
				}
			}
			moveNavImg();
			showActiveImage();
		}
		function showActiveImage(){
			$('#albumslider .albumslider-nav-items li').each(function(i){
							if(i==nav_items_nc){
								$(this).addClass("albumslider-nav-items-active");
								
								var src = $('img', this).attr('src').replace('thumbs','resized');
								var image = "<img src='"+src+"'>";
								$('#albumslider .albumslider-image').fadeOut(400, function(){
																$('#albumslider .albumslider-image').html(image);
																					});
								$('#albumslider .albumslider-image').fadeIn(400);
							} else {
								$(this).removeClass("albumslider-nav-items-active");
							}
						});
		}
		function moveNavImg(){
			var sl = 0;
			if( nav_loop == 1 ) {
				sl = Math.max(0, (nav_items_nc-2)*nav_item_width);
			} else {
				if( nav_items_nc < 3 ) {
					sl = 0;
				} else if( nav_items_nc + 2 >= nav_items_count ) {
					sl = (nav_items_count-2)*nav_item_width;
				} else {
					sl = (nav_items_nc-2)*nav_item_width;
				}
			}
			$('#albumslider .albumslider-nav-items').animate({scrollLeft: sl}, 
						300, 'swing', //swing/linear, 
						function(){
							// Animation complete.
							});
		}
		
		//
		init();
		showActiveImage(0);
	};
})(jQuery);
